#ifndef WCCNETWORKACCESSMANAGER_H
#define WCCNETWORKACCESSMANAGER_H

#include "wccnetworkrequestbase.h"
#include "wccapi_export.h"
#include <QObject>
#include <QList>

QT_BEGIN_NAMESPACE
class QNetworkAccessManager;
class QFile;
class QUrl;
class QScriptEngine;
class QScriptValue;
class QNetworkReply;
class QAuthenticator;
QT_END_NAMESPACE

namespace WccNameSpace{
namespace Internal {

class WccResponse;

class WCCAPI_EXPORT WccNetworkAccessManager : public QObject
{
    Q_OBJECT
public:
    WccNetworkAccessManager(QObject *parent = 0);
    ~WccNetworkAccessManager();

    bool isConnectedToInternet();
    void postFile(const int requestId, const QString &url, const QString &file, const QString &contentType = "", bool auth = true);
    void sendZipToRemoteCompiler(int requestId, const QString &url, const QString &zipFileName);
    void get(int requestId, const QString &url);
    void ssoFormForumNokia(int requestId, const QString &url);
    void ssoAuthForumNokia(int requestId, const QString &url, const QString &cookies);
    void downloadBinary(int requestId, const QString &url, const QString &path);
    void signIn(int requestId, const QString &url, const QString &cookie);
    void cancelAllRequests();
    void blessWithCookie(QNetworkRequest& req);

    QByteArray m_sessionCookie;
    WccNetworkRequestBase* requestById(int requestId);
    void setProxy(const QString &host, quint16 port, const QString &username, const QString &password);
    void setSystemProxy();
    void setNoProxy();
    // xxx utils to move elsewhere
    typedef QList< QPair<QString, QString> > StringPairs;
    QScriptValue parseJson(QString msg);
    StringPairs parsePropertiesReply(const QString &propertiesreply);
    void parsePostFileReply(const QString &replymsg, QString &projectid, QString &log, QString &buildStatus);

signals:
    void jsResponse(WccResponse *response);

private slots:
    void requestFinished(QNetworkReply *reply);
    void sslErrors(QNetworkReply *reply, const QList<QSslError> &errors);
    void slotAuthenticationRequired(QNetworkReply * reply, QAuthenticator * authenticator);

private:
    QNetworkAccessManager *m_netAccessManager;
    QList< WccNetworkRequestBase * > *m_requests;
    QScriptEngine* m_scriptengine;
};

}
}
#endif // WCCNETWORKACCESSMANAGER_H
